/**
 * 将非负整数 num 转换为其对应的英文表示。
 * https://leetcode-cn.com/problems/integer-to-english-words/
 */
public class NumToWorld {

    private static String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    private static String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private static String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private static String[] thousands = {"", "Thousand", "Million", "Billion"};


    public static String getWorld(int num){
        if(num==0){
            return "zero";
        }
        StringBuffer buffer=new StringBuffer();
        for(int i=3,fool=1000000000;i>=0;i--,fool/=1000){
            int curNum=num/fool;
            if(curNum!=0){
                num -= curNum * fool;
                StringBuffer curr = new StringBuffer();
                toEnglish(curNum,curr);
                curr.append(thousands[i]).append(" ");
                buffer.append(curr);
            }
        }
        return buffer.toString().trim();
    }
    public static void toEnglish(int num,StringBuffer buffer){
        if(num==0){
            return;
        }
        if(num<10){
            buffer.append(singles[num]).append(" ");
        }else if(num<20){
            buffer.append(teens[num-10]).append(" ");
        }else if(num<100){
            buffer.append(tens[num/10]).append(" ");
            toEnglish(num%10,buffer);
        }else if(num<10000){
            buffer.append(singles[num/100]).append("Hundred").append(" ");
            toEnglish(num%100,buffer);
        }
    }

    public static void main(String[] args) {
        System.out.println(getWorld(999));
    }
}
